﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.SECURITY.BLL;

namespace KPIS.GERP.SECURITY.App_UI.UserControls
{
    public partial class Users : System.Web.UI.UserControl
    {
        private UserBLL userBLL = new UserBLL();
        private HrmEmployeeBLL empBLL = new HrmEmployeeBLL();
        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        private SsMng ssMng = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSeq.Text = DataBinder.Eval(DataItem, "USER_SEQ").ToString();
                    //// ชื่อ  สกุล
                    cmbEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "USER_NAME").ToString(), DataBinder.Eval(DataItem, "EMP_SEQ").ToString()));
                    cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "EMP_SEQ").ToString();
                    txtUserCode.Text = DataBinder.Eval(DataItem, "USER_CODE").ToString();
                    txtPassword.Text = DataBinder.Eval(DataItem, "USER_PASSWORD").ToString();
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            Nullable<int> EmpTypeSeq = string.IsNullOrEmpty(Session["EMPTYPE"].ToString()) ? (int?)null : Int32.Parse(Session["EMPTYPE"].ToString());

            cmbEmployee.Items.Clear();

            DataSet ds = empBLL.GetEmployeeInfo((int?)null, EmpTypeSeq, (int?)null, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = dt.Rows[i]["FULL_NAME"].ToString();
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["POS_TEXT"].ToString();
                tdPosName.Width = Unit.Pixel(100);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(50);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["WORKING_JOB_SITE_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(150);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            userBLL.Insert(Int32.Parse(cmbEmployee.SelectedValue)
                           , txtUserCode.Text.Trim()
                           , txtPassword.Text.Trim()
                           , "A"
                           , ssMng.EmpSeqI);//Int32.Parse(Session["EmpSeq"].ToString()));
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            userBLL.Update(Int32.Parse(lblSeq.Text.Trim())
                           , Int32.Parse(cmbEmployee.SelectedValue)
                           , txtUserCode.Text.Trim()
                           , txtPassword.Text.Trim()
                           , "A"
                           , ssMng.EmpSeqI);// Int32.Parse(Session["EmpSeq"].ToString()));
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if(string.IsNullOrEmpty(e.Value)) return;

            var dt = empBLL.GetEmployeeInfo(Convert.ToInt32(e.Value), null, null, "").Tables[0];

            if(dt.Rows.Count == 0) return;

            txtUserCode.Text = dt.Rows[0]["CITIZEN"] as string;
        }

         
    }
}